package cn.yunyichina.reservation.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by Lullaby on 2016/8/23.
 */
public class ResponseUtil {

    private static final Logger logger = LoggerFactory.getLogger(ResponseUtil.class);

    private static final String RESPONSE_APPLICATION_JSON = "application/json; charset=utf-8";

    private static final String RESPONSE_TEXT_PLAIN = "text/plain; charset=utf-8";

    public static void response(Object data, HttpServletResponse response) {
        response.setContentType(RESPONSE_APPLICATION_JSON);
        try (PrintWriter writer = response.getWriter()) {
            writer.print(data);
        } catch (IOException e) {
            logger.error("[ResponseUtil.response]", e);
        }
    }

    public static void response(String data, HttpServletResponse response) {
        try (PrintWriter writer = response.getWriter()) {
            writer.print(data);
        } catch (IOException e) {
            logger.error("[ResponseUtil.response]", e);
        }
    }

    public static void responseText(Object data, HttpServletResponse response) {
        response.setContentType(RESPONSE_TEXT_PLAIN);
        try (PrintWriter writer = response.getWriter()) {
            writer.print(data);
        } catch (IOException e) {
            logger.error("[ResponseUtil.responseText]", e);
        }
    }

}
